package com.truyentranh.utils.sqllite.datasource;

import java.io.File;

import com.truyentranh.data.HaiBLMetalData;
import com.truyentranh.utils.Logger;
import com.truyentranh.utils.sqllite.helper.SQLiteHaiBLMetalDataCachedHelper;
import com.truyentranh.utils.sqllite.helper.SQLiteTopicHaiBLHelper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

@SuppressLint("NewApi")
public class ComicsCachedDataSource {

	// Database fields
	private SQLiteDatabase database;
	private SQLiteHaiBLMetalDataCachedHelper dbHelper;
	
	public ComicsCachedDataSource(Context context) {
		dbHelper = new SQLiteHaiBLMetalDataCachedHelper(context);
	}
	
	public void open() throws SQLException {
		Logger.logMe("open data base");
		database = dbHelper.getWritableDatabase();

		if (!isTableExists(SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS, true)) {
			dbHelper.onCreate(database);
		}
	}
	
	public boolean isTableExists(String tableName, boolean openDb) {
		try {
			if (openDb) {
				if (database == null || !database.isOpen()) {
					database = dbHelper.getReadableDatabase();
				}

				if (!database.isReadOnly()) {
					database.close();
					database = dbHelper.getReadableDatabase();
				}
			}

			Cursor cursor = database.rawQuery("select * from '" + tableName + "'", null);
			if (cursor != null) {
				cursor.close();
			}
			return true;
		} catch (Exception e) {

		}
		return false;
	}
	
	public void close() {
		if(database != null && database.isOpen()){
			database.close();
		}
		database = null;
		dbHelper.close();
	}
	
	public boolean deletedComic(int topicId){
		if (database == null) {
			open();
		}
		
		Logger.logMe(">>> deleted id" + topicId);		
		boolean deleted = database.delete(SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS, SQLiteHaiBLMetalDataCachedHelper.COLUMN_TOPIC_ID + "='" + topicId + "'", null) > 0;
						
		close();
		return deleted;
	}
	
	public boolean deletedComic(HaiBLMetalData data){
		if (database == null) {
			open();
		}
		
		boolean deleted = false;
		if(data != null){
			final String id =  data.getId();
			Logger.logMe(">>> deleted id"+id);
			if(id != null && !"".equals(id) && !"-1".equals(id)){
				deleted = database.delete(SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS, SQLiteHaiBLMetalDataCachedHelper.COLUMN_ID + "='" + data.getId()+ "'", null) > 0;
			}
		}		
		close();
		return deleted;
	}

	public boolean insertComicCached(HaiBLMetalData dataSQLObjects, TopicHaiBlData haiBlData) {
		if (database == null) {
			open();
		}

		ContentValues values = new ContentValues();
		final String id =  dataSQLObjects.getId();
		long mIdInsert = -1;
		if(id != null && !"".equals(id) && !"-1".equals(id) && haiBlData != null && haiBlData.getId() != -1){
			values.put(SQLiteHaiBLMetalDataCachedHelper.COLUMN_ID, id);
			values.put(SQLiteHaiBLMetalDataCachedHelper.COLUMN_CAPTION, dataSQLObjects.getTitleDescription());
			values.put(SQLiteHaiBLMetalDataCachedHelper.COLUMN_IMAGESPATH, dataSQLObjects.getImageUrl());
			values.put(SQLiteHaiBLMetalDataCachedHelper.COLUMN_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
			values.put(SQLiteHaiBLMetalDataCachedHelper.COLUMN_TOPIC_ID,haiBlData.getId());
			
			mIdInsert = database.insertWithOnConflict(SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS, null, values, SQLiteDatabase.CONFLICT_IGNORE);
			database.close();
			database = null;
		}
		close();
		
		Logger.logMe("Insert HaiBlMetal done!!! ID >>>"+id);
		return mIdInsert != -1;
	}
	
	public Cursor getComicDataSQL(int topicId) {
		if (database == null) {
			open();
		}

		Cursor cursor = database.rawQuery(" Select * "
				+ " From  " + SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS
				+ " Where " + SQLiteHaiBLMetalDataCachedHelper.COLUMN_TOPIC_ID + "="+topicId, null);		

		try {
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();				
				return cursor;
			}
			return null;
		} finally {	
			database.close();
			database = null;
			close();
		}
	}

	public Cursor getComicDataSQL() {
		if (database == null) {
			open();
		}

		Cursor cursor = database.rawQuery(" Select * "
				+ " From  " + SQLiteHaiBLMetalDataCachedHelper.TABLE_COMICS, null);		

		try {
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();				
				return cursor;
			}
			return null;
		} finally {	
			database.close();
			database = null;
			close();
		}
	}
}
